using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wojna.Client.Play;
using System.ServiceModel;
using System.Threading;

namespace Wojna.Client
{
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Single, 
        UseSynchronizationContext = false)]
    public partial class Graj : Form, IPlayCallback
    {
        private Wojna.Client.Play.PlayClient client;
        private SynchronizationContext context;

        public Graj()
        {
            InitializeComponent();
        }

        public void JoinGame()
        {
            try
            {
                client = new PlayClient(new System.ServiceModel.
                    InstanceContext(this));
                client.Open();
                client.JoinGame("Sopel");
                client.Close();
            }
            catch (Exception) { }
            finally 
            {
                if (client != null && client.State == System.ServiceModel.
                    CommunicationState.Opened)
                {
                    client.Close();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.JoinGame();
        }

        #region IPlayCallback Members

        public void NotifyPlayerJoinedGame(string request)
        {
            this.textBox1.Text = request;
        }

        public void NotifyPlayerLeftGame(string request)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void NotifyWhoWon(string request)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void NotifyWhatHappend(Karta kartaPolozona)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}